﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HFController;

namespace HFWebManage.Lawyer
{
    public partial class Lawyer : HFController.BagePages
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //判断管理员有没有登录，如果没登录，踢出
            this.checkAdminIsLogin();

            //是否首次加载
            if (!Page.IsPostBack)
            {
                dropCityID.Items.Insert(0, new ListItem("--请选择--", "0"));
                this.bindProvinceID();


                //获取Action参数，判断是修改还是删除
                string Action = this.GetQuery("action");

                switch (Action)
                {
                    case "modify":
                        //表示修改
                        this.bindmodel();
                        break;
                    case "delete":
                        //表示删除
                        int ID = int.Parse(this.GetQuery("ID"));
                        LawyerController.getController().doDelegeLawyer(ID);
                        break;
                }
            }
        }

        /// <summary>
        /// 绑定省份
        /// </summary>
        public void bindProvinceID()
        {
            dropProvinceID.DataSource = CityController.getController().getProvinceList(100);
            dropProvinceID.DataValueField = "ID";
            dropProvinceID.DataTextField = "ProvinceName";
            dropProvinceID.DataBind();
            dropProvinceID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 省份选定出发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void dropProvinceID_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.bindCityID();
        }

        /// <summary>
        /// 绑定城市
        /// </summary>
        public void bindCityID()
        {
            dropCityID.DataSource = CityController.getController().getCityList(100, dropProvinceID.SelectedValue);
            dropCityID.DataValueField = "ID";
            dropCityID.DataTextField = "CityName";
            dropCityID.DataBind();
            dropCityID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 绑定修改
        /// </summary>
        private void bindmodel()
        {
            int ID = int.Parse(this.GetQuery("ID"));
            //得到这条记录
            hf.Model.Lawyer model = LawyerController.getController().getLawyerModel(ID);
            //输入框赋值
            tbUID.Text = model.UID;
            tbPWD.Text = model.PWD;
            tbEmail.Text = model.Email;
            dropProvinceID.SelectedValue = model.ProvinceID.ToString();
            dropCityID.SelectedValue = model.CityID.ToString();
            rbsex.SelectedValue = model.Sex.ToString();
            tbBrithday.Text = model.Brithday.ToString();
            tbMobile.Text = model.Mobile;
            tbAddress.Text = model.Address;
            tbFax.Text = model.Fax;
            tbPost.Text = model.Post;
            tbWebSite.Text = model.WebSite;
            tbRealName.Text = model.RealName;
            tbAreas.Text = model.Areas;
            tbLanguage.Text = model.Language;
            tbContent.Text = model.Content;
            tbLawyerNo.Text = model.LawyerNo;
            tbLawyerFirm.Text = model.LawyerFirm;
            tbPoint.Text = model.Point.ToString();
            tbHits.Text = model.Hits.ToString();
            imgHeadPic.ImageUrl = this.getUpFileFolder + model.HeadPic;
            tbPhone.Text = model.Phone;
            rblisIndex.SelectedValue = model.isIndex.ToString();
            tbContent.Text = model.Content;

            //隐藏域ID赋值
            IDS.Value = model.ID.ToString();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string Action = this.GetQuery("action");
            if (Action == "modify")
            {
                LawyerController.getController().doAdminModifyLawyer(int.Parse(IDS.Value),
                    tbUID.Text,
                    tbPWD.Text,
                    int.Parse(tbPoint.Text),
                    int.Parse(tbHits.Text),
                    int.Parse(rblisIndex.SelectedValue),
                    int.Parse(dropProvinceID.SelectedValue),
                    int.Parse(dropCityID.SelectedValue),
                    int.Parse(rbsex.SelectedValue),
                    DateTime.Parse(tbBrithday.Text),
                    tbEmail.Text,
                    tbPhone.Text,
                    tbMobile.Text,
                    tbAddress.Text,
                    tbFax.Text,
                    tbPost.Text,
                    tbWebSite.Text,
                    tbRealName.Text,
                    tbAreas.Text,
                    tbLanguage.Text,
                    tbContent.Text,
                    tbLawyerNo.Text,
                    tbLawyerFirm.Text,
                    FileUpload1.ToString(),
                    int.Parse(rbisDayStar.SelectedValue)
                    );
            }
            else
            {
                LawyerController.getController().doAdminAddLawyer(tbUID.Text,
                    tbPWD.Text,
                    int.Parse(dropProvinceID.SelectedValue),
                    int.Parse(dropCityID.SelectedValue),
                    int.Parse(rbsex.SelectedValue),
                    DateTime.Parse(tbBrithday.Text),
                    tbEmail.Text,
                    tbPhone.Text,
                    tbMobile.Text,
                    tbAddress.Text,
                    tbFax.Text,
                    tbPost.Text,
                    tbWebSite.Text,
                    tbRealName.Text,
                    tbAreas.Text,
                    tbLanguage.Text,
                    tbContent.Text,
                    tbLawyerNo.Text,
                    tbLawyerFirm.Text,
                    FileUpload1.ToString(),
                    int.Parse(tbPoint.Text),
                    int.Parse(tbHits.Text),
                    int.Parse(rblisIndex.SelectedValue),
                    int.Parse(rbisDayStar.SelectedValue)
                    );
            }
        }
    }
}